package com.zw.pdm.module.production.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.designtask.DsnProStruct;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.production.service.ProductionAddService;
import com.zw.pdm.module.production.service.ProductionSheetService;
import com.zw.pdm.module.report.model.Project;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_MAKETYPE;
import static com.zw.pdm.constant.Constant.GROUP_SPECTYPE;


/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName ProductionAddController.java
 * @Description TODO 制作件清单创建
 * @createTime 2020年10月06日 16:43:00
 */
@Controller
@RequestMapping("/production/add")
@SuppressWarnings("all")
public class ProductionAddController extends BaseController {

    @Autowired
    private ProductionSheetService productionSheetService;
    @Autowired
    private ProductionAddService productionAddService;
    @Autowired
    private DictCommonService dictCommonService;



    /**  myf点击左侧菜单栏的制作件清单提报时默认查询下拉专业类型的下拉框*/
    @GetMapping("/make")
    @RequiresPermissions(value = {"production:make:view"}, logical = Logical.OR)
    public String makePage(Model model){
        model.addAttribute("specials",dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("makeTypes", dictCommonService.findDictByGroupName(GROUP_MAKETYPE));
        return "production/add/makePage";
    }

    /** myf根据项目编号和专业类型查询部分 */
    @GetMapping("/part/list")
    @ResponseBody
    @RequiresPermissions(value = {"production:make:view"}, logical = Logical.OR)
    public List<DsnProStruct> partList(String code,String type) {
        List<DsnProStruct> list = productionSheetService.findDrawPartByProjectCode(code, type);
        return list;
    }

    /** myf根据手动输入的项目编号模糊查询设计类型的项目信息*/
    @GetMapping("/designProject/list")
    @ResponseBody
    public List<Project> designProList(String q) {
        return productionSheetService.findDesignProjectByCode(q);
    }



    /** myf上传附件到临时文件夹 */
    @ResponseBody
    @PostMapping("/upload")
    @RequiresPermissions(value = {"production:make:edit"}, logical = Logical.OR)
    public JsonResult uploadExtends(@RequestParam("file") MultipartFile file) {
        String path = productionSheetService.uploadMakeTemp(file);
        return success().setData(path);
    }

    /**
     * myf保存制作件
     */
    @ResponseBody
    @PostMapping("/make/save")
    @RequiresPermissions(value = {"production:make:edit"}, logical = Logical.OR)
    public JsonResult saveMake(MakeSheet sheet, String[] drawNo, Integer[] drawNum, String[] drawName,String[] makeType, String[] remark, String[] url,Integer[] orderNum) {
        int i = productionAddService.saveMake(sheet, drawNo, drawNum, drawName, makeType, remark, url, orderNum);
        if(-2==i){
            return JsonResult.success("当前项目下的专业和部分已经提报过了！请走变更流程！",-2);
        }
        return jsonResult(i);
    }


}
